package com.yjt.fresh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseResultEntity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.adpater.AdpRvAtyMyCollection;
import com.yjt.fresh.api.ApiDeleteCollection;
import com.yjt.fresh.api.ApiGetCollections;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanCollection;
import com.yjt.fresh.bean.BeanCoupon;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.utils.PreferenceManager;
import com.yjt.fresh.widget.CustomToastResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.nfc.tech.MifareUltralight.PAGE_SIZE;

/**
 * 我的收藏
 * Created by mcc on 2018/8/8.
 */

public class AtyMyCollection extends BaseSimpleActivity {
    private static final int MASS_DELETE = -1;//批量删除
    @BindView(R.id.rlBack)
    RelativeLayout mRlBack;
    @BindView(R.id.tvAppAtyMyCollectionEdit)
    TextView mTvEdit;
    @BindView(R.id.tvAppAtyMyCollectionCancel)
    TextView mTvCancel;
    @BindView(R.id.rvAppAtyMyCollection)
    RecyclerView mRv;
    @BindView(R.id.srlAppAtyMyCollection)
    SwipeRefreshLayout mRefreshLayout;
    @BindView(R.id.cbAppAtyMyCollectionSelectAll)
    CheckBox mCbSelectAll;
    @BindView(R.id.tvAppAtyMyCollectionMassDelete)
    TextView mTvDelete;
    @BindView(R.id.llAppAtyMyCollectionBottomDelete)
    LinearLayout mLlBottomDelete;
    private int mNextRequestPage = 1;
    private boolean isLoadMore;
    private AdpRvAtyMyCollection mAdpRvAtyMyCollection;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_my_collection;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);

    }

    @Override
    protected void initEvent() {
        initRv();
        getData();
        initClick();
    }

    private void initClick() {
        mTvEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mAdpRvAtyMyCollection.getData().size() > 0){
                    mLlBottomDelete.setVisibility(View.VISIBLE);
                    mTvEdit.setVisibility(View.GONE);
                    mTvCancel.setVisibility(View.VISIBLE);
                    mAdpRvAtyMyCollection.isShowCb(true);
                }
            }
        });

        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLlBottomDelete.setVisibility(View.GONE);
                mTvEdit.setVisibility(View.VISIBLE);
                mTvCancel.setVisibility(View.GONE);
                mAdpRvAtyMyCollection.isShowCb(false);
                mAdpRvAtyMyCollection.isCheck(false);
                mCbSelectAll.setChecked(false);
            }
        });

        mAdpRvAtyMyCollection.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tvAppRecycleItemAtyMyCollectionDelete){
                    deleteCollection(position);
                }else if (view.getId() == R.id.llAppRecycleItemAtyMyCollectionContent){
                    Intent intent = new Intent(mContext,AtyProductDetail.class);
                    intent.putExtra(MyConstants.GOODS_ID,mAdpRvAtyMyCollection.getItem(position).getDETAIL_ID());
                    startActivity(intent);
                }
            }
        });
        mCbSelectAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mAdpRvAtyMyCollection.isCheck(isChecked);
            }
        });

        mTvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteCollection(MASS_DELETE);
            }
        });
        mRlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mNextRequestPage = 1;
                isLoadMore = false;
                getData();
            }
        });

        mAdpRvAtyMyCollection.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                isLoadMore = true;
                getData();
            }
        }, mRv);
    }

    HttpOnNextListener<BaseResultEntity> mDeleteCollectionListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            CustomToastResult.show("删除成功");
            mLlBottomDelete.setVisibility(View.GONE);
            mTvEdit.setVisibility(View.VISIBLE);
            mTvCancel.setVisibility(View.GONE);
            mAdpRvAtyMyCollection.isShowCb(false);
            mAdpRvAtyMyCollection.isCheck(false);
            mCbSelectAll.setChecked(false);
            mNextRequestPage = 1;
            isLoadMore = false;
            getData();
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };
    private void deleteCollection(int position) {
        ApiDeleteCollection apiDeleteCollection = new ApiDeleteCollection(mDeleteCollectionListener,mContext,true,false,false);
        Map<String,Object> map = new HashMap<>();
        map.put("SESSION_ID",PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        if (position == MASS_DELETE){
            String deleteID = "";
            List<BeanCollection> data = mAdpRvAtyMyCollection.getData();
            ArrayList<String> deleteIDs = new ArrayList<>();
            for (int i = 0; i < data.size(); i++) {
                if (data.get(i).isCheck()){
                    deleteIDs.add(data.get(i).getCOLLECT_ID());
                }
            }
            if (deleteIDs.size() > 0){
                for (int i = 0; i < deleteIDs.size(); i++) {
                    if (i != deleteIDs.size() - 1){
                        deleteID += deleteIDs.get(i) + ",";
                    }else {
                        deleteID += deleteIDs.get(i);
                    }
                }
                map.put("COLLECT_IDS",deleteID);
                apiDeleteCollection.setMap(map);
                HttpManager.getInstance().doHttpDeal(apiDeleteCollection);
            }else {
                Toast.makeText(mContext, "请先选择要删除的收藏", Toast.LENGTH_SHORT).show();
            }
        }else {
            map.put("COLLECT_IDS",mAdpRvAtyMyCollection.getData().get(position).getCOLLECT_ID());
            apiDeleteCollection.setMap(map);
            HttpManager.getInstance().doHttpDeal(apiDeleteCollection);
        }
    }

    HttpOnNextListener<List<BeanCollection>> mCollectionListener = new HttpOnNextListener<List<BeanCollection>>() {
        @Override
        public void onNext(List<BeanCollection> beanCollections) {
            mAdpRvAtyMyCollection.setEmptyView(R.layout.view_empty);
            if (isLoadMore) {
                setData(false, beanCollections);
            } else {
                setData(true, beanCollections);
                mRefreshLayout.setRefreshing(false);
            }
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };

    private void setData(boolean isRefresh, List<BeanCollection> data) {
        mNextRequestPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdpRvAtyMyCollection.getData().clear();
            mAdpRvAtyMyCollection.addData(data);
            mAdpRvAtyMyCollection.setEnableLoadMore(false);
        } else {
            if (size > 0) {
                mAdpRvAtyMyCollection.addData(data);
                mAdpRvAtyMyCollection.loadMoreComplete();
            }else {
                mAdpRvAtyMyCollection.loadMoreEnd(true);
            }
        }
    }

    private void getData() {
        ApiGetCollections apiGetCollections = new ApiGetCollections(mCollectionListener, mContext, true, false, false);
        Map<String, Object> map = new HashMap<>();
        map.put("currentPage", mNextRequestPage);
        map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        apiGetCollections.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiGetCollections);
    }

    private void initRv() {
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        mRv.setLayoutManager(manager);
        List<BeanCollection> beanCollections = new ArrayList<>();
        mAdpRvAtyMyCollection = new AdpRvAtyMyCollection(R.layout.app_recycle_item_aty_my_collection, beanCollections);
        mRv.setAdapter(mAdpRvAtyMyCollection);
    }

}
